package com.springboot.service.impl;

import com.springboot.dto.PatientOrderInfo;
import com.springboot.mapper.*;
import com.springboot.model.*;
import com.springboot.service.SuccessOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class SuccessOrderInfoServiceImpl implements SuccessOrderInfoService {
    @Autowired
    OrderFormMapper orderFormMapper;

    @Autowired
    DoctorInfoFormMapper doctorInfoFormMapper;

    @Autowired
    DoctorFieldFormMapper doctorFieldFormMapper;

    @Autowired
    UserLoginFormMapper userLoginFormMapper;

    @Autowired
    PatientInfoFormMapper patientInfoFormMapper;

    /**
     * @ description:展示患者的病情描述和预约详情
     * @ param      :[bookId]
     * @ return     :com.springboot.dto.PatientOrderInfo
     * @ date       :2020/12/23

    */
    @Override
    public PatientOrderInfo showPatientOrderInfo(Integer bookId) {
        PatientOrderInfo patientOrderInfo = new PatientOrderInfo();
        try {
            OrderForm orderForm = new OrderForm();
            try {
                orderForm  =  orderFormMapper.selectByPrimaryKey(bookId);
            }
            catch (Exception e1)
            {
                e1.printStackTrace();
                System.out.println("orderFormMapper.selectByPrimaryKey(bookId) 出错");
            }
            patientOrderInfo.setOrderForm(orderForm);
            Integer doctorId=  orderForm.getDoctorId();
            DoctorInfoForm doctorInfoForm = new DoctorInfoForm();
            try {
                doctorInfoForm = doctorInfoFormMapper.selectByPrimaryKey(doctorId);
            }
            catch (Exception e2)
            {
                e2.printStackTrace();
                System.out.println("doctorInfoFormMapper.selectByPrimaryKey(doctorId) 出错");
            }
            patientOrderInfo.setDoctorInfoForm(doctorInfoForm);
            Integer patientId = orderForm.getPatientId();
            UserLoginForm userLoginForm = new UserLoginForm();
            try {
                userLoginForm = userLoginFormMapper.selectByPrimaryKey(patientId);
            }
            catch (Exception e3)
            {
                e3.printStackTrace();
                System.out.println("userLoginFormMapper.selectByPrimaryKey(patientId) 出错");
            }
            String patientNmae = userLoginForm.getUserName();
            String patientTel = userLoginForm.getUserTel();
            String patientSex = userLoginForm.getUserSex();
            patientOrderInfo.setPatientName(patientNmae);
            patientOrderInfo.setPatientTel(patientTel);
            patientOrderInfo.setPatientSex(patientSex);
            ArrayList<DoctorFieldForm> doctorFieldForms = new ArrayList<>();
            try {
                doctorFieldForms = doctorFieldFormMapper.selectByDoctorId(doctorId);
            }
            catch (Exception e4)
            {
                e4.printStackTrace();
                System.out.println("doctorFieldFormMapper.selectByDoctorId(doctorId) 出错");
            }
            patientOrderInfo.setDoctorFieldForms(doctorFieldForms);
        }
        catch (Exception e)
        {
            System.out.println("SuccessOrderInfoServiceImpl 出错");
            System.out.println("成功预约信息业务出错");
            e.printStackTrace();
        }

        PatientInfoForm patientInfoForm = new PatientInfoForm();
        try {
            patientInfoForm = patientInfoFormMapper.selectByPrimaryKey(bookId);
            patientOrderInfo.setPatientInfoForm(patientInfoForm);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("SuccessOrderInfoServiceImpl-selectByPrimaryKey错误:bookID="+" "+bookId);
        }
        finally {
            return patientOrderInfo;
        }
    }
}
